local hx__zuhao = fk.CreateSkill {
  name = "hx__zuhao",
}

local HX = require "packages/huixiangkill/hxfs"

--删去右键查看技能的qml，刚好用于租号
Fk:addQmlMark{
  name = "zuhaomod",
  how_to_show = function(name, value)
    if type(value) == "string" then
      return Fk:translate(value)
    elseif type(value) == "table" then
      return tostring(#value)
    end
    return " "
  end,
  qml_path = "packages/huixiangkill/qml/HxDetailBox"
}

hx__zuhao:addEffect(fk.RoundStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__zuhao.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    HX.ZuHaoSelect(room, player, "摸牌", "视为", "伤害", hx__zuhao.name)
  end,
})



local zuhaomod_patten = { ---租号mod词条
  ---无注释的为每行12个
  "摸牌","视为","伤害","+1","-1","加一","减一","牌名","点数","花色","类型","颜色",
  "增加","减少","无视","距离","次数","永久","直到","本次","至少","至多","弃置","获得",
  "回复","失去","各","递减","递增","下回合","本回合","此阶段","响应","抵消","体力上限","护甲",
  "濒死","脱离","进入","离开","销毁","失效","无效","下一次","下次","上次","上一次","随机",
  "秘密","体力值","基本","锦囊","装备","武器","立即","额外","所有","即将","使用","打出",
  "受到","造成","防止","展示","一次","两次","三次","0","1","2","3","4",
  "5","6","7","8","9","观看","一张","两张","改为","执行","相同","不同",
  "移去","枚","个","选择","势力","游戏","场上","牌堆","一项","当作","令其","移除",
  "防止","取消","准备","判定","结束","重置","重铸","交换","魏","蜀","吴","群",
  "神","范围","交给","攻击","【酒】","【闪】","【杀】","【桃】","当前回合","当前","拼点","红桃",
  "黑桃","梅花","方块","♠","♥","♣","♦","开始","红色","黑色","一名","等量",
  "横置","翻面","弃置","区域","死亡","休整","x","X","y","Y","任意","发动",
  "未发动","被","若为","是","否","是否","记录","取整","向上","向下","一半","正面",
  "此牌","最大","最小","复原","连环","状态","恢复", --7个
}

local zuhaomod_player = { ---可开启租号mod的名单
  ---每行10个
  "庆思书","谎如昨日，嗤笑今朝","COMP－3","COMP－4","焉民","焉","幻矩","八云立层云涌","sjj","jxfpwe",
  "kriyo","汝九泉之下，定会感朕之情","1111",
}
local file = "./packages/zuhaomod_player.txt"
-- 用这个不算邪道吧
local function safeReadFile()
  local success, lines = pcall(io.lines, file)
  if success then
    for line in lines do
      local playerName = line:match("^%s*(.-)%s*$")
      if playerName ~= "" then
        table.insertIfNeed(zuhaomod_player, playerName)
      end
    end
  end
end
safeReadFile()

local patterns = "./packages/zuhaomod.txt"
-- 中奖了，再来一遍
local function safeReadFile2()
  local success, lines = pcall(io.lines, patterns)
  if success then
    for line in lines do
      local playerName = line:match("^%s*(.-)%s*$")
      if playerName ~= "" then
        table.insertIfNeed(zuhaomod_player, playerName)
      end
    end
  end
end
safeReadFile2()


local zuhaomod_mode = { ---默认不开启租号mod的模式
  "aaa_role_mode",
  "hx__role2_mode",
  "hx__1v3_mode",
  "hx__1v2_mode",
  "hx__2v2_mode",
  "hx__skillchanging_mode",
  "hx__simulator_mode",
  "marm_mode",
}

hx__zuhao:addEffect(fk.RoundStart, {
  mute = true,
  is_delay_effect = true,
  global = true,
  priority = 2,
  can_trigger = function(self, event, target, player, data)
    if table.contains(zuhaomod_mode, player.room.settings.gameMode) then return false end
    local canplayers = table.filter(player.room.players, function(p) return p.id > 0 and table.contains(zuhaomod_player, p._splayer:getScreenName()) end)
    if #canplayers > 0 and player.room:getTag("zuhaomod") ~= "close" then
      return player.room:getTag("zuhaomod_ask") ~= "cannot"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setTag("zuhaomod_ask", "cannot")
    local targets = table.filter(room.alive_players, function(p) return p.id > 0 end)
    local to = table.random(targets)
    room:sendLog({type = ":huixiang__zuhaomod-intro"})
    local result = room:askToJointChoice(to, {
      players = targets,
      choices = {"yes", "no", "close_zuhaomod"},
      prompt = "你正在与其他人投票决定是否开启本轮的租号mod，<b>所有人都选“是”</b>才会开启哦",
      skill_name = "是否开启本轮的 <b>租号mod</b>？（点击战报查看详细规则）",
    })
    local cannot = 0
    local close = 0
    for _, p in ipairs(targets) do
      if result[p] == "no" and p.id > 0 then
        cannot = cannot + 1
      end
      if result[p] == "close_zuhaomod" and p.id > 0 then
        close = close + 1
      end
    end
    if close == 0 then
      if cannot == 0 then
        room:setTag("zuhaomod", "yes")
      elseif cannot ~= 0 then
        room:setTag("zuhaomod", "no")
      end
    else
      room:setTag("zuhaomod", "close")
    end
    if room:getTag("zuhaomod") == "yes" then
      room:doBroadcastNotify("ShowToast", "本轮租号mod已开启！！祝你们游戏愉快！")
      room:doBroadcastNotify("ShowToast", "目前词条数为"..#zuhaomod_patten.."个，欢迎提出新词条~")
      room:setBanner("@[:]huixiang__zuhaomod-intro","huixiang__zuhaomod-intro")
      for _, p in ipairs(room.alive_players) do
        if p.id > 0 then
          local random_mods = {}
          while #random_mods < 3 do
            local random = math.random(1, #zuhaomod_patten)
            if not table.contains(random_mods, zuhaomod_patten[random]) then
              table.insert(random_mods, zuhaomod_patten[random])
            end
          end
          room:setPlayerMark(p, "@[zuhaomod]zuhaomod_patten-round", random_mods)
          HX.ZuHaoSelect(room, p, random_mods[1],random_mods[2], random_mods[3], self.name,3)
        end
      end
    end
  end,
  can_refresh = function(self, event, target, player, data)
    return player.room:getTag("zuhaomod") ~= "close"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setTag("zuhaomod_ask", "can")
  end,
})


Fk:loadTranslationTable{
  ["@[:]huixiang__zuhaomod-intro"] = "租号mod",
  ["huixiang__zuhaomod-intro"] = "介绍",
  [":huixiang__zuhaomod-intro"] = "本局出现了可开启租号mod的玩家，故每轮开始时将进行投票是否开启当前轮次的租号mod，注意，<b>所有人均选“是”</b>才会开启租号mod，<b>有人选“否”</b>则会关闭本轮的租号mod，但是一旦<b>有人选了“关闭本局”</b>，则会永久关闭本局的租号mod。"..
  "<br/>若mod成功开启，则每名角色将依次执行下面的效果，不同的是，技能将不仅仅局限于“摸牌”、“视为”、“伤害”这三个关键词，且有保底机制，关键词超多，多多体验吧~~<br/>"..
  "<b><br/>你可以展示至多x张武将牌，若如此做，你根据下面的关键词依次选择这些武将牌："..
  "<br/>①“摸牌”；"..
  "<br/>②“视为”；"..
  "<br/>③“伤害”。"..
  "<br/>若被选武将的某个技能的描述中包含对应关键词，<br/>则你本轮获得此技能；若被选武将无此类技能，你减1点体力上限。"..
  "（x为你的体力上限且至少为3，至多为100）</b>",
  ["@[zuhaomod]zuhaomod_patten-round"] = "<font color=\"#E0DB2F\">租号词条</font>",
  ["@[:]zuhao_skills-round"] = "<font color=\"#87CEFA\">租号技能</font>",
  ["#zuhao_log"] = "%from的租号关键词是： %arg ，展示的武将牌为： %arg2",
  ["#zuhao_true_log"] = "%from在【租号】效果的3个词条中<font color= 'green'>选对</font>的武将分别是：<br/>词条1： %arg ，<br/>词条2： %arg2 ，<br/>词条3： %arg3 。",
  ["#zuhao_false_log"] = "%from在【租号】效果的3个词条中<font color= 'red'>选错</font>的武将分别是：<br/>词条1： %arg ，<br/>词条2： %arg2 ，<br/>词条3： %arg3 。",
  ["#zuhao_falsenum_log"] = "%from本次【租号】将减少的体力上限是 %arg 点。",
  ["close_zuhaomod"] = "关闭本局",
}


return hx__zuhao